﻿using JNPF.Basics.Models;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 数据同步
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.09.03
    /// </summary>
    public class DbSyncBll
    {
        private DbSyncIService service = new DbSyncService();
        private DbLinkBll dbLinkBll = new DbLinkBll();
        private DatabaseService databaseService = new DatabaseService();
        /// <summary>
        /// 批量写入
        /// </summary>
        /// <param name="dbConnectionFrom">数据库连接 From</param>
        /// <param name="dbConnectionTo">数据库连接To</param>
        /// <param name="table"></param>
        public async Task<string> ImportTableData(string dbConnectionFrom, string dbConnectionTo, string table)
        {
            try
            {
                var linkFrom = await dbLinkBll.GetInfo(dbConnectionFrom);
                var linkTo = await dbLinkBll.GetInfo(dbConnectionTo);
                await service.ImportTableData(linkFrom, linkTo, table);
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        /// 判断表中是否有数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public bool IsNullDataByTable(DbLinkEntity entity, string table)
        {
            var data = databaseService.GetData(entity, table);
            if (data.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
    }
}
